package com.juzi.wopu.dao;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.juzi.wopu.domain.Employee;
import com.juzi.wopu.query.BaseQuery;
import com.juzi.wopu.query.PageList;

@SuppressWarnings("all")
public class BaseDao<T> extends HibernateDaoSupport{

	/**
	 * 保存
	 * @param t
	 */
	public void save(T t){
		getHibernateTemplate().save(t);
	}
	
	/**
	 * 修改
	 * @param t
	 */
	public void update(T t){
		getHibernateTemplate().update(t);
	}
	
	/**
	 * 根据id删除指定的对象
	 * @param entityClass
	 * @param id
	 */
	public void delete(Class<T> entityClass,Serializable id){
		T t=get(entityClass,id);
		if(t!=null){
			getHibernateTemplate().delete(t);
		}
	}

	/**
	 * 根据id获取指定的一个对象
	 * @param entityClass
	 * @param id
	 * @return
	 */
	public T get(Class<T> entityClass,Serializable id){
		return (T) getHibernateTemplate().get(entityClass, id);
	}
	
	/**
	 * 获取所有的对象
	 * @param entityClass
	 * @return
	 */
	public List<T> getAll(Class<T> entityClass){
		return getHibernateTemplate().loadAll(entityClass);
	}
	
	public List findCacheByHql(final String hql) {//没有条件参数的可以使用此方法
		return getHibernateTemplate().executeWithNativeSession(
				new HibernateCallback<List>() {
					public List doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query query = session.createQuery(hql);
						// 查询缓存生效
						query.setCacheable(true);
						return query.list();
					}
				});
	}
	
	/**
	 * 分页
	 * @param baseQuery
	 * @return
	 */
	public PageList findByQuery(final BaseQuery baseQuery){
		Long countLong = getHibernateTemplate().executeWithNativeSession(new HibernateCallback<Long>() {

			@Override
			public Long doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createQuery(baseQuery.getCountHql());
				List paramList = baseQuery.getParamList();
				for(int i=0;i<paramList.size();i++){
					query.setParameter(i, paramList.get(i));
				}
				return (Long)query.uniqueResult();
			}
		});
		
		if(countLong.intValue()==0){
			return new PageList();
		}
		int currentPage = baseQuery.getCurrentPage();
		int pageSize = baseQuery.getPageSize();
		
		final PageList pageList=new PageList(currentPage,pageSize,countLong.intValue());
		
		List rows = getHibernateTemplate().executeWithNativeSession(new HibernateCallback<List>() {
			@Override
			public List doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createQuery(baseQuery.getLimitHql());
				List paramList = baseQuery.getParamList();
				for(int i=0;i<paramList.size();i++){
					query.setParameter(i, paramList.get(i));
				}
				
				int firstResult=(pageList.getCurrentPage()-1) * pageList.getPageSize();
				int maxResule = pageList.getPageSize();
				query.setFirstResult(firstResult);
				query.setMaxResults(maxResule);
				return query.list();
			}
		});
		pageList.setRows(rows);
		return pageList;
	}
	
	
	public List findByhql(String hql,Object...objects){
		return getHibernateTemplate().find(hql,objects);
	}
	
	
	public void executeSql(String sql){
		Connection connection=getSession().connection();
		PreparedStatement preparedStatement=null;
		try {
			preparedStatement=connection.prepareStatement(sql);
			preparedStatement.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public ResultSet executeQuerySql(String sql){
		Connection connection=getSession().connection();
		PreparedStatement preparedStatement=null;
		ResultSet resultSet=null;
		try {
			preparedStatement=connection.prepareStatement(sql);
			resultSet=preparedStatement.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return resultSet;
	}

}

